<template>
	<view class="wrap">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<input v-model.trim="nameValue" placeholder-class="line" placeholder="请填写收货人姓名" />
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<input v-model.trim="phoneValue"  type="number" placeholder-class="line" placeholder="请填写收货人手机号" />
			</view>
			<view class="item area-item" @click="handleShowPicker">
				<view class="left">所在地区</view>
				<view class="area-right">
					<text class="area-value" v-if="areaValue">{{areaValue}}</text>
					<text class="area-placeholder" v-else>省市区县、乡镇等</text>
				</view>
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text" v-model.trim="detailValue" placeholder-class="line" placeholder="街道、楼牌等" />
			</view>
		</view>
		<view class="bottom">
			<view class="tag">
				<view class="left">标签</view>
				<view class="right">
					<text  v-for="(item,i) in tagsList" :key="i" :class="{tags:true,active:currentTagsIndex==i}" @click="handleTagsClick(i)">{{item}}</text>
				</view>
			</view>
			<view class="default">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">提醒：每次会默认使用该地址</view>
				</view>
				<view class="right">
					<u-switch active-color="#0DA297" v-model="checked"></u-switch>
				</view>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<u-picker mode="region" confirm-color="#0DA297" ref="uPicker" v-model="pickerShow"
			@confirm="handlePickerConfirm" />
		<!-- #endif -->
		<!-- #ifdef MP-ALIPAY -->
		<u-select v-model="pickerShow" label-name="value" value-name="code" mode="mutil-column-auto" :list="list" @confirm="handlePickerConfirm"></u-select>	
		<!-- #endif -->
		
		<view class="btn-wrap">
			<view class="btn">
				<uv-btn shape="circle" @click="handleSaveClick" text="保存"></uv-btn>
			</view>
			<view class="bottom-safe-area"></view>
		</view>
		<view class="bottom-safe-area"></view>
	</view>
</template>

<script>
	// #ifdef	MP-ALIPAY
	import list from '@/common/city.js';
	// #endif
	
	export default {
		data() {
			return {
				//picker
				// #ifdef	MP-ALIPAY
				list:list,//省市区数据
				// #endif
				pickerShow: false,
				//tags
				tagsList:['公司','家','学校','其它'],
				//main data
				nameValue: '',
				phoneValue: "",
				areaValue: "",
				detailValue: '',
				checked: false,
				currentTagsIndex:null,
				//编辑的id
				id:null,
			};
		},
		onLoad(options) {
			if(options.id){
				//是编辑进来的
				this.id=options.id;
				this.getDataByIdRequest();
				
			};
			
		},
		methods: {
			//通过id查询编辑的数据
			getDataByIdRequest(){
				this.$u.api.modifyAddress({addr_id:this.id}).then(res=>{
					let obj=res.date;
					this.nameValue=res.date.consignee;
					this.phoneValue=res.date.phone;
					this.areaValue=res.date.area;
					this.detailValue=res.date.address;
					this.checked=res.date.default_addr==2;
					let currentTagsIndex=null;
					switch(res.date.tab) {
						case '1':
							currentTagsIndex=0;
							break;
						case '2':
							currentTagsIndex=1;
							break;
						case '3':
							currentTagsIndex=2;
							break;
						case '4':
							currentTagsIndex=3;
							break;
						default:
					};
					this.currentTagsIndex=currentTagsIndex;
				}).catch(()=>{});
			},
			//Picker
			handleShowPicker() {
				this.pickerShow = true;
			},
			handlePickerConfirm(e) {
				// #ifdef	MP-WEIXIN
				this.areaValue = e.province.label + ' ' + e.city.label + ' ' + e.area.label;
				// #endif
				
				// #ifdef	MP-ALIPAY
				this.areaValue= e[0].label + ' ' + e[1].label + ' ' + e[2].label; 
				// #endif
				
			},
			//tags
			handleTagsClick(i){
				this.currentTagsIndex=i;
			},
			//save
			handleSaveClick(){

				//verify
				if(this.$utils.isAllEmpty(this.nameValue)){
					this.$utils.toast('收货人姓名不能为空');
				}else if(this.$utils.isAllEmpty(this.phoneValue)){
					this.$utils.toast('收货人手机号不能为空');
				}else if(this.$utils.isAllEmpty(this.areaValue)){
					this.$utils.toast('所在地区不能为空');
				}else if(this.$utils.isAllEmpty(this.detailValue)){
					this.$utils.toast('详细地址不能为空');
				}else if(!this.$utils.isPhone(this.phoneValue)){
					this.$utils.toast('手机号格式不正确');
				}else if(this.currentTagsIndex==null){
					this.$utils.toast('请选择标签');
				}else{
					//request
					this.sureRequest();
				};
				
			},
			sureRequest(){
				let data={
					
					name:this.nameValue,
					phone:this.phoneValue,
					address:this.areaValue,
					detailed:this.detailValue,
					defaults:this.checked?2:1,
					label:this.currentTagsIndex==null?'':(this.currentTagsIndex+1+''),
				};
				//如果是编辑的时候，增加一个参数做修改意思，否则就是添加操作;
				if(this.id){
					data.addr_id=this.id;
				};   
				this.$u.api.updateAddress(data).then(res=>{
					if(res.code == 1){
						this.$utils.toastCallback(res.msg,()=>{
							this.$utils.back();
						},true);
					}else{
						this.$utils.modal({
							content:res.msg,
							showCancel:false,
						});
					};
				}).catch(()=>{});
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: $u-light-color;
		font-size: 28rpx;
	}

	.wrap {

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 180rpx;
				}

				input {
					text-align: left;
				}

				.area-right {
					flex: 1;
				}
			}

			.address {
				padding: 0;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding-left: 40rpx;

			background-color: #ffffff;
			font-size: 28rpx;

			.tag {
				display: flex;

				.left {
					width: 160rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx $u-border-color;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 28rpx;
						align-items: center;
						justify-content: center;
						color: $u-content-color;
						line-height: 1;
						
					}
					

					.plus {
						//padding: 10rpx 0;
					}
				}
			}

			.default {
				margin-top: 50rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}

				.right {
					padding-right: 24rpx;
				}
			}
		}
	}

	.btn-wrap {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		padding: 20rpx 0;

		.btn {
			padding: 0 24rpx;
		}
	}

	.area-item {
		line-height: 50rpx !important;
		padding: 24rpx 0;
	}
	.area-placeholder{
		color:#c0c4cc;
		font-size: 28rpx;
	}
	.tags.active{
		background-color:#0da297 !important;
		color:#fff !important;
		border-color:#0da297 !important;
	}
</style>
